package com.hoperun.product.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.honeywell.aidc.BarcodeFailureEvent;
import com.honeywell.aidc.BarcodeReadEvent;
import com.honeywell.aidc.BarcodeReader;
import com.honeywell.aidc.ScannerUnavailableException;
import com.honeywell.aidc.TriggerStateChangeEvent;
import com.honeywell.aidc.UnsupportedPropertyException;
import com.hoperun.distributecloth.R;
import com.hoperun.product.adapter.ClothListAdapter;
import com.hoperun.product.adapter.ClothListAdapter.onDeleteClickListener;
import com.hoperun.product.base.BaseActivity;
import com.hoperun.product.entity.ClothEntity;
import com.hoperun.product.net.IServiceImpl;
import com.hoperun.product.net.RequestServiceImpl;
import com.hoperun.product.util.SharedPreUtil;
import com.hoperun.product.view.SimpleDialog;
import com.hoperun.product.view.SimpleDialog.OnNegativeClickListener;
import com.hoperun.product.view.SimpleDialog.OnPositiveClickListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity implements OnClickListener {

	private final static int REQUEST_TRAYSETTING = 1;
	private CheckBox detailCheckBox;
	private TextView jobNumTv;
	private TextView classesTv;
	private EditText trayNoEt;
	private Button setTrayBtn;
	private TextView trayWeightTv;
	private TextView packageTv;
	private EditText clothTktNoEt;
	private Button getContactBtn;
	private Button getTrayBtn;
	private TextView countsTv;
	private TextView clothWeightTv;
	private ListView mListView;
	private Button finishBtn;
	List<ClothEntity> mClothList = new ArrayList<ClothEntity>();
	ClothListAdapter mAdapter;
	// 包装类型
	private String mPackageType = "";
	// 包装重量
	private String mPackageWeight = "";
	// 托盘重量
	private String mTrayWeight = "";

	// 下机重量
	private String mFinishWeight = "";
	// 用户userbean的json数据
	private String mUserBean = "";
	// 员工工号
	private String mJobNum = "";
	// 三联单号
	private String mClothTktNo = "";

	private String mTrayNo = "";

	private String mTrayCount = "";
	private com.honeywell.aidc.BarcodeReader barcodeReader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		initDate();
		initView();
		initEvent();
		//initBarCode();
		getInitInfo();
		

		
	}
	
	
	
	
	

	private void initDate() {
		mUserBean = (String) SharedPreUtil.getValue(this, SharedPreUtil.USERBEAN, "");
		mJobNum = (String) SharedPreUtil.getValue(this, SharedPreUtil.YHM, "");
	}

	private void initView() {
		detailCheckBox = (CheckBox) findViewById(R.id.detailCheckBox);
		jobNumTv = (TextView) findViewById(R.id.jobNumTv);
		classesTv = (TextView) findViewById(R.id.classesTv);
		trayNoEt = (EditText) findViewById(R.id.trayNoEt);
		setTrayBtn = (Button) findViewById(R.id.setTrayBtn);
		trayWeightTv = (TextView) findViewById(R.id.trayWeightTv);
		packageTv = (TextView) findViewById(R.id.packageTv);
		clothTktNoEt = (EditText) findViewById(R.id.clothTktNoEt);

		getContactBtn = (Button) findViewById(R.id.getContactBtn);
		getTrayBtn = (Button) findViewById(R.id.getTrayInfoBtn);
		countsTv = (TextView) findViewById(R.id.countsTv);
		clothWeightTv = (TextView) findViewById(R.id.weightTv);
		mListView = (ListView) findViewById(R.id.listview);
		finishBtn = (Button) findViewById(R.id.finishBtn);

		jobNumTv.setText(mJobNum);

		mAdapter = new ClothListAdapter(this, mClothList);
		// 设置分割线
		mListView.setDivider(getResources().getDrawable(R.color.line));
		// 设置分割线高度
		mListView.setDividerHeight(1);
		mListView.setAdapter(mAdapter);


	}

	private void initEvent() {
		setTrayBtn.setOnClickListener(this);
		getTrayBtn.setOnClickListener(this);
		getContactBtn.setOnClickListener(this);
		finishBtn.setOnClickListener(this);
		// trayNoEt.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		// trayNoEt.setOnEditorActionListener(new OnEditorActionListener() {
		//
		// @Override
		// public boolean onEditorAction(TextView v, int actionId, KeyEvent
		// event) {
		// // 点击搜索键
		// if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null &&
		// event.getAction() == KeyEvent.ACTION_UP)) {
		// startGetTrayByTrayNo();
		// }
		// return false;
		// }
		// });
		trayNoEt.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {

					startGetTrayByTrayNo();

					return true;
				}
				return false;
			}
		});

		clothTktNoEt.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {

					startCheckClothTktNo();

					return true;
				}
				return false;
			}
		});

		mAdapter.setOnDeleteClickListener(new onDeleteClickListener() {

			@Override
			public void onClick(int position) {
				// mClothList.remove(position);
				// mAdapter.notifyDataSetChanged();
				// // 刷新匹数和重量
				// refeshCountsAndWeight();
				String clothTktNo = mClothList.get(position).CLOTHTKTNO;
				deleteCloth(clothTktNo);
			}
		});
		clothTktNoEt.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					String trayWeightStr = trayWeightTv.getText().toString().trim();
					String packageStr = packageTv.getText().toString().trim();
					if (TextUtils.isEmpty(trayWeightStr) || TextUtils.isEmpty(packageStr)) {
						SimpleDialog.forceShow(MainActivity.this, getResources().getString(R.string.you_not_input_weight_and_package_goto_input), new OnNegativeClickListener() {

							@Override
							public void onNegativeClick() {
								clothTktNoEt.clearFocus();
							}
						}, new OnPositiveClickListener() {

							@Override
							public void onPositiveClick() {

								Intent intent = new Intent(MainActivity.this, TraySettingActivity.class);
								startActivityForResult(intent, REQUEST_TRAYSETTING);
							}
						});
					}
				}

			}
		});

	}
	
	
	
	
//
//	private void initBarCode(){
//		barcodeReader = LoginActivity.getBarcodeObject();
//		if (barcodeReader != null) {
//
//			// register bar code event listener
//			barcodeReader.addBarcodeListener(this);
//
//			// set the trigger mode to client control
//			try {
//				barcodeReader.setProperty(BarcodeReader.PROPERTY_TRIGGER_CONTROL_MODE, BarcodeReader.TRIGGER_CONTROL_MODE_AUTO_CONTROL);
//			} catch (UnsupportedPropertyException e) {
//				Toast.makeText(this, "Failed to apply properties", Toast.LENGTH_SHORT).show();
//			}
//			// register trigger state change listener
//			barcodeReader.addTriggerListener(this);
//
//			Map<String, Object> properties = new HashMap<String, Object>();
//			// Set Symbologies On/Off
//			properties.put(BarcodeReader.PROPERTY_CODE_128_ENABLED, true);
//			properties.put(BarcodeReader.PROPERTY_GS1_128_ENABLED, true);
//			properties.put(BarcodeReader.PROPERTY_QR_CODE_ENABLED, true);
//			properties.put(BarcodeReader.PROPERTY_CODE_39_ENABLED, true);
//			properties.put(BarcodeReader.PROPERTY_DATAMATRIX_ENABLED, true);
//			properties.put(BarcodeReader.PROPERTY_UPC_A_ENABLE, true);
//			properties.put(BarcodeReader.PROPERTY_EAN_13_ENABLED, false);
//			properties.put(BarcodeReader.PROPERTY_AZTEC_ENABLED, false);
//			properties.put(BarcodeReader.PROPERTY_CODABAR_ENABLED, false);
//			properties.put(BarcodeReader.PROPERTY_INTERLEAVED_25_ENABLED, false);
//			properties.put(BarcodeReader.PROPERTY_PDF_417_ENABLED, false);
//			// Set Max Code 39 barcode length
//			properties.put(BarcodeReader.PROPERTY_CODE_39_MAXIMUM_LENGTH, 10);
//			// Turn on center decoding
//			properties.put(BarcodeReader.PROPERTY_CENTER_DECODE, true);
//			// Enable bad read response
//			properties.put(BarcodeReader.PROPERTY_NOTIFICATION_BAD_READ_ENABLED, true);
//			// Apply the settings
//			barcodeReader.setProperties(properties);
//		}
//
//	}
	
	
	
	
	
	
	
	
	

	/**
	 * 刷新匹数和重量
	 */
	private void refeshCountsAndWeight() {
		int counts = mClothList.size();
		double weight = 0;
		for (ClothEntity cloth : mClothList) {
			weight += Double.parseDouble(cloth.FINISHWEIGHT);
		}
		clothWeightTv.setText(String.format("%.2f", weight) + "");
		countsTv.setText(counts + "");
	}

	/**
	 * 准备 根据托盘号带出托盘数据
	 */
	private void startGetTrayByTrayNo() {

		if (isTrayNoEmpty()) {
			return;
		}
		// 转换为大写字母
		trayNoEt.setText(mTrayNo.toUpperCase());// 转换
		mTrayNo = trayNoEt.getText().toString().trim();
		getTrayByTrayNo(mTrayNo);
	}

	/**
	 * 开始准备校验三联单号,也是添加三联单号的唯一路口
	 */
	private void startCheckClothTktNo() {

		if (isClothTktNoEmpty()) {
			return;
		}
		// 转换为大写字母
		trayNoEt.setText(mTrayNo.toUpperCase());// 转换
		mTrayNo = trayNoEt.getText().toString().trim();
		checkClothTktNo(mClothTktNo,mTrayNo,mTrayCount);
	}

	/**
	 * 开始准备细分校验
	 */
	private void startCheckClothTktNoByDetail() {

		if (isClothTktNoEmpty()) {
			return;
		}
		if (isTrayNoEmpty()) {
			return;
		}
		checkClothTktNoByDetail(mClothTktNo, mTrayNo);
	}

	/**
	 * 开始准备新增一条数据
	 */
	private void startAddCloth() {

		if (isClothTktNoEmpty()) {
			return;
		}
		if (isTrayNoEmpty()) {
			return;
		}
		addCloth(mClothTktNo, mTrayNo, mTrayCount,mTrayWeight, mPackageType, mPackageWeight);
	}

	/**
	 * 准备完成更新
	 */
	private void startFinishUpdate() {

		if (isTrayNoEmpty()) {
			return;
		}
		finishUpdate(mTrayNo, mTrayWeight, mPackageType, mPackageWeight);

	}

	/**
	 * 初始化信息，主要是获取班次
	 */
	private void getInitInfo() {
		showWait();
		RequestServiceImpl.getInitInfo(mMIPHandler, IServiceImpl.GET_INIT_INFO, mUserBean);
	}

	/**
	 * 根据托盘号带出托盘数据
	 * 
	 * @param trayNo
	 */
	private void getTrayByTrayNo(String trayNo) {
		showWait();
		String userBean = (String) SharedPreUtil.getValue(this, SharedPreUtil.USERBEAN, "");
		RequestServiceImpl.getTrayByTrayNo(mMIPHandler, IServiceImpl.GET_TRAY_BY_TRAYNO, userBean, trayNo);
	}

	/**
	 * 校验三联单号（普通校验）
	 * 
	 * @param clothTktNo
	 */
	private void checkClothTktNo(String clothTktNo,String trayNo,String trayCount) {
		showWait();
		RequestServiceImpl.checkClothTktNo(mMIPHandler, IServiceImpl.CHECK_CLOTHTKTNO, clothTktNo,trayNo,trayCount,clothWeightTv.getText().toString());
	}

	/**
	 * 校验三联单号 细分校验
	 * 
	 * @param clothTktNo
	 * @param trayNo
	 */
	private void checkClothTktNoByDetail(String clothTktNo, String trayNo) {
		showWait();
		RequestServiceImpl.checkClothTktNoByDetail(mMIPHandler, IServiceImpl.CHECK_CLOTHTKTNO_BY_DETAIL, clothTktNo, trayNo);
	}

	/**
	 * 新增一个数据
	 *
	 * @param clothTktNo
	 * @param trayNo
	 * @param trayWeight
	 * @param packageType
	 * @param packageWeight

	 */
	private void addCloth(String clothTktNo, String trayNo,String trayCount, String trayWeight, String packageType, String packageWeight) {
		showWait();
		RequestServiceImpl.addCloth(mMIPHandler, IServiceImpl.ADD_CLOTH, mUserBean, clothTktNo, trayNo, trayCount,trayWeight, packageType, packageWeight);
	}

	/**
	 * 删除一个数据
	 * 
	 * @param clothTktNo
	 */
	private void deleteCloth(String clothTktNo) {
		showWait();
		RequestServiceImpl.deleteCloth(mMIPHandler, IServiceImpl.DELETE_CLOTH, mUserBean, clothTktNo);
	}

	/**
	 * 完成更新
	 * 
	 * @param trayNo
	 */
	private void finishUpdate(String trayNo, String trayWeight, String packageType, String packageWeight) {
		showWait();
		RequestServiceImpl.finishUpdate(mMIPHandler, IServiceImpl.FINISH_UPDATE, mUserBean, trayNo, trayWeight, packageType, packageWeight);
	}

	/**
	 * 清除页面所有信息
	 */
	private void clearAllInfo() {
		//mPackageType = "";
		//mPackageWeight = "";
		//mTrayWeight = "";
		mFinishWeight = "";
		trayNoEt.setText("");

		//trayWeightTv.setText("");
		//packageTv.setText("");
		clothTktNoEt.setText("");

		countsTv.setText("0");
		clothWeightTv.setText("0");
		mClothList.clear();
		mAdapter.notifyDataSetChanged();

	}

	/**
	 * 判断三联单号（胚布条码）是否为空 如果为空弹出提示框
	 * 
	 * @return
	 */
	private boolean isClothTktNoEmpty() {
		mClothTktNo = clothTktNoEt.getText().toString().trim();
		if (TextUtils.isEmpty(mClothTktNo)) {
			SimpleDialog.forceShow(this, getResources().getString(R.string.please_input_cloth_code));
			clothTktNoEt.requestFocus();
			return true;
		}
		return false;
	}

	/**
	 * 判断托盘号是否为空 如果为空弹出提示框
	 * 
	 * @return
	 */
	private boolean isTrayNoEmpty() {
		mTrayNo = trayNoEt.getText().toString().trim();
		if (TextUtils.isEmpty(mTrayNo)) {
			SimpleDialog.forceShow(this, getResources().getString(R.string.please_input_trayno));
			trayNoEt.requestFocus();
			return true;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setTrayBtn:
			// 只有当托盘重量和包装为空的情况，或者，列表为空的情况下，才能修改托盘信息
			if (TextUtils.isEmpty(mTrayWeight) || !(mClothList.size() > 0)) {
				Intent intent = new Intent(MainActivity.this, TraySettingActivity.class);
				startActivityForResult(intent, REQUEST_TRAYSETTING);
			} else {
				SimpleDialog.show(this, getResources().getString(R.string.please_delete_all_data_then_edit_trayinfo));
			}
			break;
		case R.id.getContactBtn:

			if (isClothTktNoEmpty()) {
				return;
			}
			ContactInfoActivity.actionStart(this, mClothTktNo);
			break;
		case R.id.getTrayInfoBtn:
			if (isClothTktNoEmpty()) {
				return;
			}
			TrayInfoActivity.actionStart(this, mClothTktNo);
			break;
		case R.id.finishBtn:
			startFinishUpdate();
			break;
		default:
			break;
		}

	}

	// @Override
	// public boolean dispatchKeyEvent(KeyEvent event) {
	// // toast("dispatchKeyEvent" + event.getKeyCode() + "");
	// // 拦截物理键盘事件
	// // 拦截回车键
	// if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
	// if (event.getAction() == KeyEvent.ACTION_DOWN) {
	// // mScanGun.isMaybeScanning(event.getKeyCode(), event);
	// return true;
	// }
	// return true;
	// }
	// return super.dispatchKeyEvent(event);
	// }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_TRAYSETTING:
			if (resultCode == RESULT_OK) {
				String packageStr = data.getStringExtra(TraySettingActivity.EXTRA_ANSWER_PACKAGE);
				mTrayWeight = data.getStringExtra(TraySettingActivity.EXTRA_ANSWER_WEIGHT);
				mPackageType = data.getStringExtra(TraySettingActivity.EXTRA_ANSWER_PACKAGE_TYPE);
				mPackageWeight = data.getStringExtra(TraySettingActivity.EXTRA_ANSWER_PACKAGE_WEIGHT);
				trayWeightTv.setText(mTrayWeight);
				packageTv.setText(packageStr);
			}

			break;

		default:
			break;
		}
	}

	@Override
	public void onPostHandle(int requestType, Object objBody, boolean error) {

		String msg = "";
		String opt = "";
		if (error) {
			switch (requestType) {
			case IServiceImpl.GET_INIT_INFO:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						String classes = jsonObj.optString("BC");
						classesTv.setText(classes);
					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case IServiceImpl.GET_TRAY_BY_TRAYNO:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						Gson gson = new Gson();
						List<ClothEntity> clothList = new ArrayList<ClothEntity>();
						clothList = gson.fromJson(jsonObj.optJSONArray("result").toString(), new TypeToken<List<ClothEntity>>() {
						}.getType());
						if (clothList != null) {
							// 每次获取到数据，到要清空原来的数据
							mClothList.clear();
							// mClothList.addAll(clothList);
							// 过滤掉三联单号为空的数据，2018年4月23日修改
							for (int i = 0; i < clothList.size(); i++) {
								ClothEntity cloth = clothList.get(i);

								if (cloth.CLOTHTKTNO != null) {
									mClothList.add(cloth);
									
								}
							}
							mAdapter.notifyDataSetChanged();
							// 刷新匹数和重量
							refeshCountsAndWeight();
							mTrayWeight = clothList.get(0).TRAYWEIGHT;
							String packageStr = clothList.get(0).PACKAGETYPE;
							//add by zhuyu 20190715
							mTrayCount = clothList.get(0).TRAYCOUNT;
							mPackageType = packageStr.split("\\(")[0];
							mPackageWeight = packageStr.split("\\(")[1].split("\\)")[0];
							trayWeightTv.setText(mTrayWeight);
							packageTv.setText(packageStr);
							// 坯布条码获取焦点
							clothTktNoEt.requestFocus();

						}

					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			case IServiceImpl.CHECK_CLOTHTKTNO:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						// 判断粗分选择框是否勾选，若没勾选，需要细分校验
						if (!detailCheckBox.isChecked()) {
							// 同时需要满足当前列表中有数据，才去调细分的接口
							if (mClothList.size() > 0) {
								startCheckClothTktNoByDetail();
							} else {
								startAddCloth();
							}

						} else {
							startAddCloth();
						}

					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					} else if ("2".equals(opt)) {
						SimpleDialog.show(this, msg);
					} else if ("3".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case IServiceImpl.CHECK_CLOTHTKTNO_BY_DETAIL:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						// 细分校验通过，准备新增当前数据
						startAddCloth();

					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					} else if ("2".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			case IServiceImpl.ADD_CLOTH:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					//edit by zhuyu 20190716 添加一个类型：4， 多一个提示框
					if ("0".equals(opt)||"4".equals(opt)) {
						// 增加数据成功后 重新掉接口刷新界面
						startGetTrayByTrayNo();
						clothTktNoEt.setText("");
						if("4".equals(opt)){
							SimpleDialog.show(this, msg);
						}


					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					} else if ("2".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case IServiceImpl.DELETE_CLOTH:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						// 删除数据成功 重新掉接口刷新界面

						startGetTrayByTrayNo();
						SimpleDialog.show(this, msg);

					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case IServiceImpl.FINISH_UPDATE:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						// 完成更新数据成功
						clearAllInfo();
						SimpleDialog.show(this, msg);
						trayNoEt.requestFocus();
					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					} else if ("2".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}

		} else {

			// toast("连接服务器失败！");
			if (objBody != null) {
				String obj = objBody.toString();
				SimpleDialog.show(this, obj);
			}
		}
		stopWait();
	}

	/************************************霍尼韦尔***************************************/
	private static final String ACTION_BARCODE_DATA = "com.honeywell.sample.action.BARCODE_DATA";
	private static final String ACTION_CLAIM_SCANNER = "com.honeywell.aidc.action.ACTION_CLAIM_SCANNER";
	private static final String ACTION_RELEASE_SCANNER = "com.honeywell.aidc.action.ACTION_RELEASE_SCANNER";
	private static final String EXTRA_SCANNER = "com.honeywell.aidc.extra.EXTRA_SCANNER";
	private static final String EXTRA_PROFILE = "com.honeywell.aidc.extra.EXTRA_PROFILE";
	private static final String EXTRA_PROPERTIES = "com.honeywell.aidc.extra.EXTRA_PROPERTIES";

	private BroadcastReceiver barcodeDataReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			int version = intent.getIntExtra("version", 0);
			if (version >= 1) {
				//ToastUtils.showLong("接收到霍尼韦尔广播数据");
				String data = intent.getStringExtra("data");

				if (!TextUtils.isEmpty(data)) {

					if (trayNoEt.hasFocus()) {
						trayNoEt.setText(data);
						startGetTrayByTrayNo();
					} else if (clothTktNoEt.hasFocus()) {
						clothTktNoEt.setText(data);
						startCheckClothTktNo();
					}

				}
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(barcodeDataReceiver, new IntentFilter(ACTION_BARCODE_DATA));
		claimScanner();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(barcodeDataReceiver);
		releaseScanner();
	}

	private void claimScanner() {
		Bundle properties = new Bundle();

		properties.putBoolean("DPR_DATA_INTENT", true);
		properties.putString("DPR_DATA_INTENT_ACTION", ACTION_BARCODE_DATA);

		sendBroadcast(new Intent(ACTION_CLAIM_SCANNER).putExtra(EXTRA_SCANNER, "dcs.scanner.imager").putExtra(EXTRA_PROFILE, "MyProfile1").putExtra(EXTRA_PROPERTIES, properties));
	}

	private void releaseScanner() {
		sendBroadcast(new Intent(ACTION_RELEASE_SCANNER));
	}

	/************************************霍尼韦尔***************************************/


}
